import { Button, Card, Form, Input,message } from 'antd';
import { useNavigate, useParams } from 'react-router-dom';
import "../index.css"


import { addHospitalRequest, updataHosFromIdRequest,updataHosRequest } from '@/api/hospital/hospitalSetRequest';

import type { addHospitalParamType,updataHosParamsType } from '@/api/hospital/model/hospitalSetType';
import { useEffect } from 'react';

export default function AddOrUpdate() {
    const navigate = useNavigate()
    const { id } = useParams()
    //创建表单实例
    const [form] = Form.useForm();

    //添加医院请求回调
    const addFinishHandle = async (infinish: addHospitalParamType) => {
        //更新表单数据回调函数
        if(id){
        await updataHosRequest({id,...infinish})
        //提交成功返回hospital列表
        navigate("/syt/hospital/hospitalSet")
        //全局弹窗
        message.success('更新成功')
        return
        }
        await addHospitalRequest(infinish)
        //提交成功返回hospital列表
        navigate("/syt/hospital/hospitalSet")
        //全局弹窗
        message.success('添加成功')
    }

    //根据id发送请求填充表单数据回调
    useEffect(() => {
        const hosFromId = async () => {
            if (id) {
                const result = await updataHosFromIdRequest(id)

                form.setFieldsValue(result)
            }
        }
        hosFromId()
    }, [])

   
    


    return (

        <Card>
            <Form
                form={form}
                name="basic"
                labelCol={{ span: 3, }}
                wrapperCol={{ span: 18, }}
                autoComplete="off"
                onFinish={addFinishHandle}
            >
                <Form.Item
                    label="医院名称"
                    name="hosname"
                    rules={[
                        {
                            required: true,
                            message: 'Please input your username!',
                        },
                    ]}
                >
                    <Input />
                </Form.Item>

                <Form.Item
                    label="医院编号"
                    name="hoscode"
                    rules={[
                        {
                            required: true,
                            message: 'Please input your password!',
                        },
                    ]}
                >
                    <Input />
                </Form.Item>
                <Form.Item
                    label="api基础路径"
                    name="apiUrl"
                    rules={[
                        {
                            required: true,
                            message: 'Please input your password!',
                        },
                    ]}
                >
                    <Input />
                </Form.Item>
                <Form.Item
                    label="联系人姓名"
                    name="contactsName"
                    rules={[
                        {
                            required: true,
                            message: 'Please input your password!',
                        },
                    ]}
                >
                    <Input />
                </Form.Item>
                <Form.Item
                    label="联系人手机"
                    name="contactsPhone"
                    rules={[
                        {
                            required: true,
                            message: 'Please input your password!',
                        },
                    ]}
                >
                    <Input />
                </Form.Item>

                <Form.Item
                    wrapperCol={{
                        offset: 3,
                        span: 16,
                    }}
                >
                    <Button type="primary" htmlType="submit" className="mr" >
                        提交
                    </Button>
                    <Button type="primary" onClick={() => { navigate(-1) }}>
                        返回
                    </Button>
                </Form.Item>
            </Form>
        </Card>
    )
}